﻿window.SearchKey = null;
$.MGJ.ActivitiesManage = [];
(function () {
    var _this = $.MGJ.RawCode;
    $.extend($.MGJ.RawCode, {
        Execls: function () {
            var Rsearch = $("#Asearch").val();
            window.open(ACTION_URL("Execls", "ActivitiesManage") + "?Asearch=" + window.SearchKey);
        } 
    });
})(jQuery);


(function () {
    var _this = $.MGJ.ActivitiesManage;
    $.extend($.MGJ.ActivitiesManage, {
        // 预览红包
        Dialog_PreviewRedObj: null,
        Dialog_PreviewRed: function () {

            this.Dialog_PreviewRedObj = $.L.dialog({
                url: ACTION_URL("PreviewRed", "Market"),
                title: "预览红包",
                width: 800,
                height: 'auto',
                callback: function () {
                    $.L.msgError(1);
                },

                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
        },
        // 预览转盘
        Dialog_PreviewTurntableObj: null,
        Dialog_PreviewTurntable: function () {
            this.Dialog_PreviewTurntableObj = $.L.dialog({
                url: ACTION_URL("PreviewTurntable", "Market"),
                title: "预览转盘",
                width: 350,
                height: 500,
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
        },

    });
})(jQuery);
(function () {
    var _this = $.MGJ.ActivitiesManage;

    $.extend($.MGJ.ActivitiesManage, {
        conditionModel: {
            pageIndex: 1,
            pageSize: 10,
            searchKey: null,//查询条件
            showSys: 0,  //我的模板：0  系统模板：1 我的模板和系统模板：2
            classifyId: 0 // 节点ID  等于0查询全部
        },
        Ajax_GetActivtiesList: function (pageIndex, Asearch) {
            if (!Asearch) {
                window.SearchKey = removeSpace($("#Asearch").val());
            }
            _this.conditionModel.pageIndex = pageIndex;
            _this.conditionModel.Asearch = Asearch;
            $.L.ajaxHandle({
                url: ACTION_URL("GetActivtiesList", "Market"),
                data: {
                    Asearch: window.SearchKey,
                    pageIndex: _this.conditionModel.pageIndex,
                    pageSize: _this.conditionModel.pageSize
                },
                success: function (response) {
                    $("#ActivtiesList").L_bindingDataList(response);
                    $('button.viewFlagBtn').each(function (i, ele) {
                        if ($(this).text() == '禁用') {
                            $(this).parent().prev().find('.flagSpan').css("color", "green").text("已启用");
                            $(this).removeClass('btn-success').addClass('btn-danger');
                        }
                        else {
                            $(this).parent().prev().find('.flagSpan').css("color", "red").text("已禁用");
                        }

                    })
                }
            });
        },
        //启用
        Ajax_IsState: function (ele) {
            if ($(ele).hasClass('btn-success')) {
                //点击禁用按钮  实现启用状态 
                $.L.ajaxHandle({
                    url: ACTION_URL("CheckIsbindWXSMS", "Market")+"?AId="+$(ele).attr("Id")+"&Type=1",
                    success: function (data) {
                        if (data.wx == 0) {
                            $.L.msgError("请先绑定公众号信息！");
                            return;
                        }
                        else if (data.sms==0) {
                            $.L.msgError("请先绑定短信接口配置数据！");
                            return;
                        }
                        else if (!data.state) {
                            $.L.msgError("奖品名称："+data.jName + " 剩余数量不足活动发放数量！");
                            return;
                        }  
                        else {
                            $.L.ajaxHandle({
                                url: ACTION_URL("IsState", "Market") + "?Aid=" + $(ele).attr("Id") + "&Flag=" + $(ele).attr("ViewFlag"),
                                success: function (response) {
                                    if (response == 1) {
                                        $.L.msgSuccess("启用成功！");
                                        //$(ele).parent().prev().find('.flagSpan').css("color", "green").text("已启用");
                                        $(ele).removeClass('btn-success').addClass('btn-danger').text('禁用').attr('ViewFlag', '1').parent().prev().find('.flagSpan').css("color", "green").text("已启用");
                                    } else {
                                        $.L.msgError("您的金额小于活动预估计的最大金额：" + response + " 元");
                                    }
                                }
                            });
                        }
                    }
                });

            }
            else {
                //点击启用按钮 实现禁用状态
                $.L.ajaxHandle({
                    url: ACTION_URL("IsState", "Market") + "?Aid=" + $(ele).attr("Id") + "&Flag=" + $(ele).attr("ViewFlag"),
                    success: function (response) {
                        if (response == 1) {
                            $.L.msgSuccess("禁用成功！");
                            //$(ele).parent().prev().find('.flagSpan').css("color", "red").text("已禁用");
                            $(ele).removeClass('btn-danger').addClass('btn-success').text('启用').attr('ViewFlag', '0').parent().prev().find('.flagSpan').css("color", "red").text("已禁用");
                        }
                    }
                });
            }

        },
        //预览
        Ajax_ShowCode: function (ele)
        { 
            $.L.ajaxHandle({
                url: ACTION_URL("CheckIsbindWXSMS", "Market") + "?AId=" + $(ele).attr("Id")+"&Type=2",
                success: function (data) {
                
                    if (data.wx == 0) {
                        $.L.msgError("请先绑定公众号信息！");
                        return;
                    }
                    else if (data.sms == 0) {
                        $.L.msgError("请先绑定短信接口配置数据！");
                        return;
                    }
                    else {
                        
                        $("#phone").modal("show");
                   
                        //生成二维码
                        //   var qrstring = "http://tapp.m12315.com/PreView/ActivityHome?aId="+$(ele).attr('Id');
                      var qrstring = "http://www.m12315.com/app/wx/preHome?aId=" + $(ele).attr('Id');
                        //var qrstring = "http://tapp.m12315.com/wx/PreHome?aId=" + $(ele).attr('Id');
                        $("#qr_code").html("");
                        //二维码
                        $("#qr_code").qrcode({
                            width: 120, //宽度
                            height: 120, //高度
                            //text: qrsting //任意内容
                            text: qrstring
                        });
                        $('#hintText').html('请用微信扫描预览');
                        

                    }
                }
            });
        },
        //删除
        Ajax_activityDel: function (ele) {
            
            var id = $(ele).attr("Id");
      
            var ViewFlag= $(ele).prev().prev().prev().attr("ViewFlag");
            if (ViewFlag == 1) {
                $.L.msgWarning("活动已启动，不能删除！");
                return;
            }
            //
            $.L.ajaxHandle({
                url: ACTION_URL("ActivityDel", "Market") + "?Aid=" + id,
                success: function (response) {
                     
                        $.L.msgSuccess("操作成功！");
                        _this.Ajax_GetActivtiesList(_this.conditionModel.pageIndex, _this.conditionModel.Asearch);
                  
                }
            });
        },
        //编辑
        ActivtiesEdit: function (ele) {
            if ($(ele).prev().prev().attr("ViewFlag") == 1) {
                $.L.msgWarning("活动已启动，不能编辑！");
                return;
            }
            else {
                var type = $(ele).attr("ActivityType");
                var viewName = "";
                switch (type) {
                    case "1": //九宫格
                        $('.main .big-right').load("/Market/TurntableActivity?ActivityType=" + type + "&oper=edit&Aid=" + $(ele).attr("Id"));
                        break;
                    case "2"://红包
                        $('.main .big-right').load("/Market/RPActivity?ActivityType=" + type + "&oper=edit&Aid=" + $(ele).attr("Id"));
                        break;
                    case "3"://砸金蛋
                        $('.main .big-right').load("/Market/GoldEggsActivity?ActivityType=" + type + "&oper=edit&Aid=" + $(ele).attr("Id"));
                        break;
                }    
            } 
        },

        Bin_ViewFlagBtn: function (text) {

            return text == 0 ? "启用" : "禁用";
        },
        Bin_ViewFlagSpan: function (text) {

            return text == 1 ? "已启用" : "已禁用";
        },
        Bin_ViewFlagclass: function (Class) {

            return text == 0 ? "btn-success" : "btn-danger";
        },
        Bin_src: function (ele) {
         
            switch (ele) {
                case 1://大转盘
                    return "/Content/webImgs/Market/Turntable/SudokuIco.png";
                case 2://红包
                    return "/Content/webImgs/Market/redPacket/redpack.png";
                case 3://砸金蛋
                    return "/Content/webImgs/Market/GoldEggs/1.jpg";
            }
        },
        DateFormat: function (val) {
            var date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10));
            //月份为0-11，所以+1，月份小于10时补个0
            var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
            var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
            var dd = date.getFullYear() + "-" + month + "-" + currentDate;
            return dd;
        }
    });
})(jQuery);
(function () {
    var _this = $.MGJ.ActivitiesManage;
    $.extend($.MGJ.ActivitiesManage, {


    });
})(jQuery);
